using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.WebCore.CorsExtend
{
    /// <summary>
    /// 跨域设置
    /// </summary>
    public static class CorsExtension
    {
        /// <summary>
        /// 配置跨域策略
        /// </summary>
        /// <param name="services"></param>
        public static void AddCorsExt(this IServiceCollection services)
        {
            services.AddCors(policy =>
            {
                policy.AddPolicy("CorsPolicy",
                    opt => opt
                 .SetIsOriginAllowed((x) => true)
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
                .WithExposedHeaders("X-Pagination")
                .WithOrigins("http://localhost:5001"));
            });
        }

        /// <summary>
        /// 配置跨域策略生效
        /// </summary>
        /// <param name="app"></param>
        public static void UseCorsExt(this WebApplication app)
        {
            app.UseCors("CorsPolicy");
        }
    }
}
